"""
Case Type   : 兼容M*数据库time函数
Case Name   : time()函数，入参为time类型，含微秒
Create At   : 2023.01.06
Owner       : z@wan005
Description :
    1、小数位数0~6位
    2、四舍五入，最多保留6位小数
    3、清理环境
Expect      :
    1、成功
    2、四舍五入，最多保留6位小数
    3、成功
History     :modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest05(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0005"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = "--step1:小数位数0~6位;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('24:59:59.9');" \
                  "select time('24:59:59.99');" \
                  "select time('24:59:59.999');" \
                  "select time('24:59:59.9999');" \
                  "select time('24:59:59.99999');" \
                  "select time('24:59:59.999999');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 24, '行数不到29行')
        self.assertEqual('24:59:59.9', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.99', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.999', sql_res.splitlines()[12].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.999', sql_res.splitlines()[12].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.9999', sql_res.splitlines()[17].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.99999', sql_res.splitlines()[-7].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59.999999', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step2:小数位超过6位;expect:四舍五入，最多保留6位小数--"
        self.log.info(text)
        sql_cmd = "select time('24:59:59.9999994');" \
                  "select time('24:59:59.9999999');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 8, '行数不到8行')
        self.assertEqual('24:59:59.999999', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('25:00:00', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
